// 导入接口请求
import { loginApi, getUserInfoApi } from '@/api/user'

// 导入本地token数据处理
import { setToken, getToken, delToken } from '@/utils/auth'
// 模块数据
export default {
  namespaced: true,
  state: {
    token: getToken() || '',
    userInfo: {}
  },
  mutations: {
    setToken (state, payload) {
      state.token = payload
      setToken(payload)
    },
    delToken (state) {
      state.token = ''
      delToken()
    },
    getUserInfo (state, payload) {
      state.userInfo = payload
    }
  },
  actions: {
    // 获取token
    async getTokenAction ({ commit }, payload) {
      const { data } = await loginApi(payload)
      if (data.code === 0) {
        commit('setToken', data.token)
      }
      return data
    },
    // 获取用户信息
    async getUserAction ({ commit }) {
      const { data } = await getUserInfoApi()
      // console.log(data)
      // 存入用户数据
      commit('getUserInfo', data.data)
    }
  }
}
